import { useState, useEffect } from "react";

type SizeType = {
  width: number,
  height: number
}

/**
 * 获取window窗口大小
 * @param call 
 * @returns windowSize
 */
export const useWindowSize = (call: (size: SizeType) => void) => {
  const [size, setSize] = useState<SizeType>({
    width: 0,
    height: 0
  })
  useEffect(() => {
    if (!window.onresize) {
      window.onresize = () => {
        const newSize = {
          width: window.innerWidth,
          height: window.innerHeight
        }
        setSize(newSize)
        call?.(newSize)
      }
    }
  }, [])
  return size
}